package io.kiki.sba.registry.common.model.slot;

import io.kiki.sba.registry.util.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public final class SlotConfig {

    public static final String KEY_DATA_SLOT_NUM = "registry.data.slot.num";
    public static final int SLOT_NUM;
    public static final int SLOT_REPLICAS;
    public static final String Slot_Function;
    private static final Logger logger = LoggerFactory.getLogger(SlotConfig.class);
    private static final String KEY_DATA_SLOT_FUNC = "registry.data.slot.func";
    private static final String KEY_DATA_SLOT_REPLICAS = "registry.data.slot.replicas";

    static {
        SLOT_NUM = SystemUtils.getSystemInteger(KEY_DATA_SLOT_NUM, 256);
        if (SLOT_NUM <= 0) {
            throw new IllegalArgumentException("illegal " + KEY_DATA_SLOT_NUM + ":" + SLOT_NUM);
        }
        SLOT_REPLICAS = SystemUtils.getSystemInteger(KEY_DATA_SLOT_REPLICAS, 2);
        if (SLOT_REPLICAS <= 0) {
            throw new IllegalArgumentException("illegal " + KEY_DATA_SLOT_REPLICAS + ":" + SLOT_REPLICAS);
        }
        Slot_Function = SystemUtils.getSystem(KEY_DATA_SLOT_FUNC, "crc32c");
        logger.info("init slot config, {}={}, {}={}, {}={}", KEY_DATA_SLOT_NUM, SLOT_NUM, KEY_DATA_SLOT_REPLICAS, SLOT_REPLICAS, KEY_DATA_SLOT_FUNC, Slot_Function);
    }

    private SlotConfig() {
    }

    public static SlotBasicInfo slotBasicInfo() {
        return new SlotBasicInfo(SLOT_NUM, SLOT_REPLICAS, Slot_Function);
    }

}
